//
//  ClassifyView.m
//  phmall
//
//  Created by admin on 2017/10/15.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "ClassifyView.h"
#define Button_Origin_Tag 2000
@interface ClassifyView ()
{
    UIView *backView;
}
@end
@implementation ClassifyView
-(instancetype)init{
    if ([super init]) {
        self.backgroundColor=[UIColor clearColor];
    }
    return self;
}
-(void)setTitleArray:(NSMutableArray *)titleArray{
    //先清空当前视图上的所有子视图
    for (UIView * view in self.subviews) {
        [view removeFromSuperview];
    }
    backView=[[UIView alloc]init];
    [self addSubview:backView];
    backView.backgroundColor=[UIColor whiteColor];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(0);
        make.left.right.equalTo(self);
        make.height.offset(170);
    }];

    _titleArray=titleArray;
    for (int i=0; i<titleArray.count; i++) {
        
        UIButton *title=[UIButton buttonWithType:UIButtonTypeCustom];
        [title setTitle:titleArray[i] forState:UIControlStateNormal];
        [backView addSubview:title];
        NSInteger width=(SCREEN_WIDTH-100)/4;
        title.tag = Button_Origin_Tag+i;
        title.titleLabel.font=[UIFont systemFontOfSize:13];
        ViewBorderRadius(title, 5, 0, [UIColor clearColor]);
        title.backgroundColor=[UIColor groupTableViewBackgroundColor];
        [title setTitleColor:[UIColor colorWithHexString:@"#adadad"] forState:UIControlStateNormal];
        [title setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [title addTarget:self action:@selector(titleAction:) forControlEvents:UIControlEventTouchUpInside];
        [title mas_makeConstraints:^(MASConstraintMaker *make) {
            NSLog(@"%d---------%d",i/4,i%4);
            make.left.equalTo(backView).offset(20+i%4*(width+20));
            make.top.equalTo(backView).offset(20+i/4*(30+20));
            make.height.offset(30);
            make.width.offset(width);
        }];
        if (i==0) {
            title.selected=YES;
            title.backgroundColor=[UIColor colorWithHexString:@"#FF8C01"];
        }
    }
    
    UIView *lucency=[[UIView alloc]init];
    lucency.backgroundColor=[UIColor blackColor];
    lucency.alpha=0.5;
    [self addSubview:lucency];
    [lucency mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backView.mas_bottom);
        make.left.right.equalTo(backView);
        make.bottom.equalTo(self);
    }];
    
    UITapGestureRecognizer *lucencyTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lucencyAction:)];
    [self addGestureRecognizer:lucencyTap];
}
-(void)lucencyAction:(UITapGestureRecognizer *)sender{
    if(self.delegate && [self.delegate respondsToSelector:@selector(withdrawClassifyView)]){
        [self.delegate withdrawClassifyView];
    }
}
-(void)titleAction:(UIButton*)button
{
    //获取点击的是第几个button
    NSInteger index = button.tag - Button_Origin_Tag;
    [self setSelectAtIndex:index];
    if(self.delegate && [self.delegate respondsToSelector:@selector(withdrawClassifyView:)]){
        [self.delegate withdrawClassifyView:index];
    }
}

-(void)setSelectAtIndex:(NSInteger)index
{
    //1、先调整自身的视图显示
    for (int i = 0; i < self.titleArray.count; i++) {
        UIButton * bt = [self viewWithTag:i+Button_Origin_Tag];
        
        if (bt.tag-Button_Origin_Tag == index) {
            bt.selected = YES;
            bt.backgroundColor=[UIColor colorWithHexString:@"#FF8C01"];
        }else{
            bt.selected = NO;
            bt.backgroundColor=[UIColor groupTableViewBackgroundColor];
        }
    }
    
}
@end
